﻿using System;
using System.Linq;
using System.Collections.Generic;
using Model;
using DAL;
using System.Data;
namespace BLL
{
    /// <summary>
    /// PMS_Project
    /// </summary>
    public class PMS_ProjectBLL
    {
        private readonly DAL.PMS_ProjectDAL dal = new DAL.PMS_ProjectDAL();
        public PMS_ProjectBLL()
        { }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddUpdate(PMS_Project model)
        {
            if (model.id <= 0)
            {
                return dal.Add(model);
            }
            else
            {
                return dal.Update(model);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 根据项目ID查询数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public PMS_Project GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 根据项目编号查询数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public PMS_Project GetModel(string projectID, string projectType)
        {
            return dal.GetModel(projectID, projectType);
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public List<PMS_Project> GetList(string whereStr)
        {
            return dal.GetList(whereStr);
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public List<PMS_Project> GetList(PMS_ProjectQuery query)
        {
            PMS_UserBLL ubll = new PMS_UserBLL();
            PMS_User user = ubll.GetModel(query.userID.Value);
            if (query.roleID == 0)
            {
                return new List<PMS_Project>();
            }
            PMS_RoleListDAL dalR = new PMS_RoleListDAL();
            PMS_RoleList umodel = dalR.GetModel(user.roleID);
            PMS_RoleList model = dalR.GetModel(query.roleID.Value);
            if (user.roleID != 0)
            {
                if (!umodel.roleIDs.Contains(model.roleIDs))
                    return new List<PMS_Project>();
            }
            if (user.role == "管理员" || user.role.Contains("客户"))
            {
                query.userID = null;
            }   
            return dal.GetList(query, model.roleIDs);
        }


        /// <summary>
        /// 根据条件查询导出数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public DataTable GetListTable(PMS_ProjectQuery query)
        {
            PMS_UserBLL ubll = new PMS_UserBLL();
            PMS_User user = ubll.GetModel(query.userID.Value);
            if (query.roleID == 0)
            {
                return null;
            }
            PMS_RoleListDAL dalR = new PMS_RoleListDAL();
            PMS_RoleList umodel = dalR.GetModel(user.roleID);
            PMS_RoleList model = dalR.GetModel(query.roleID.Value);
            if (user.roleID != 0)
            {
                if (!umodel.roleIDs.Contains(model.roleIDs))
                    return null;
            }
            return dal.GetListTable(query, model.roleIDs);
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public List<PMS_Project> GetListS(PMS_ProjectQuery query)
        {
            string roles = "'" + query.roleID + "'";
            PMS_UserBLL ubll = new PMS_UserBLL();
            PMS_User user = ubll.GetModel(Convert.ToInt32(query.userID));
            PMS_RoleListBLL rbll = new PMS_RoleListBLL();
            if (user.roleID == 0)
            {
                return dal.GetList(query, roles);
            }
            List<PMS_RoleList> rlist = rbll.GetList(user.roleID);
            PMS_RoleList model = rlist.Where(p => p.roleID == user.roleID).FirstOrDefault();
            string[] roleIDs = model.roleIDs.Split('|');
            foreach (string m in roleIDs)
            {
                int id = Convert.ToInt32(m);
                if (id != user.roleID)
                {
                    model = new PMS_RoleList();
                    model = rlist.FirstOrDefault(p => p.roleID == id);
                    rlist.Remove(model);
                }
            }

            int rid = Convert.ToInt32(query.roleID);
            model = rlist.FirstOrDefault(p => p.roleID == rid);
            if (model == null)
            {
                roles = "'" + user.roleID + "'";
            }
            return dal.GetList(query, roles);
        }
    }
}

